package com.zgh.myexamproject;


import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.zgh.myexamproject.util.MyDBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryGamesActivity extends AppCompatActivity {
    //定义组件
    ListView listView=null;
    private MyDBHelper myDBHelper;
    private SQLiteDatabase db;
    private TextView ls_gameid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_games);
        setTitle("查看记录");
        myDBHelper = new MyDBHelper(this);
        //初始化界面
        initView();
    }

    private void initView() {
        db = myDBHelper.getReadableDatabase();
        /*db.execSQL("insert into tb_Game(gameid,gamename,gametime,gamenote)Values('974296731','王者荣耀','5.28下午','双排上分')");*/
        List<Map<String,Object>> mOrderData=new ArrayList<>();
        Cursor cursor = db.query("tb_Game", null, null, null, null, null,null);
        int resultCounts = cursor.getCount();
        if (resultCounts != 0 ) {
            while (cursor.moveToNext()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("creator", cursor.getString(cursor.getColumnIndex("creator")));
                map.put("gamename", cursor.getString(cursor.getColumnIndex("gamename")));
                map.put("gametime", cursor.getString(cursor.getColumnIndex("gametime")));
                map.put("gamenote", cursor.getString(cursor.getColumnIndex("gamenote")));
                mOrderData.add(map);
            }
        }
        listView=(ListView)findViewById(R.id.lst_orders);
        String[] from={"creator","gamename","gametime","gamenote"};
        int[] to={R.id.tv_lst_gameid,R.id.tv_lst_gamename,R.id.tv_lst_gametime,R.id.tv_lst_gamenote};
        SimpleAdapter listItemAdapter=new SimpleAdapter(QueryGamesActivity.this,mOrderData,R.layout.item_list,from,to);
        listView.setAdapter(listItemAdapter);
        db.close();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ls_gameid = view.findViewById(R.id.tv_lst_gameid);
                Bundle bundle = new Bundle();
                bundle.putString("gamer",ls_gameid.getText().toString().trim());
                /*System.out.println(bundle.getString("gamer")+"budn");*/
                Intent intent=new Intent( QueryGamesActivity.this,GamerIFActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                QueryGamesActivity.this.finish();
            }
        });
        /*tv_lst_gameid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("click..");
            }
        });*/
    }
}